* Independent analysis that will be incorporated into main pipeline once approved
* Load the data file -----------------------------------------------------------

	if "$individual_run" != "Yes" {
	di "Program Stopped: Make sure you load the parameters in master.do"
	
	}

	
* Sample restriction and final data cleaning -----------------------------------

	* Limit the months to 1-12
	keep if inrange(month, 1, 12)
	
	* Rename variables to make them shorter
	* Since Stata has a restriction on matrix name
	
	gl oldnames l12_trr_C ///
				arrest_con_cop ///
				days_iod ///
				l23_trr_C trr_C ///
				l12_trr_s_i_C l12_trr_sa_injury_C l12_trr_s_h_C l12_trr_s_hos_i_ix_C ///
				ttt_score_cut_Am  ///
				award total_w_exo_accusations ///
				prs_score_w_arrs_cut_Am ///
				i_complaint_pri_cut_Am i_complaint_sec_cut_Am i_complaints_cut_Am
				
	gl newnames NLU ///
				DAR ///
				OI ///
				LTU AU ///
				NLUSI NLUSA NLUH NLUSH ///
				TTT4m  ///
				AW COM ///
				AI ///
				COMP4m COMS4m COMI4m
	
	* Rename
	capture: rename ($oldnames) ($newnames)			
				
	* Set of variales for non-LASSO regression
	gl picked5a		///
				pre_arrest_con_cop pre_prs_score_w_arrs pre_days_iod ///
				experience dblack dwhite dhispanic dmale 
				
* Merge ANA data ---------------------------------------------------------------

	* Merge in ANA data at randomization
	merge m:1 employee_id using "{$ANA_exo}", ///
	nogen keep(1 3) 
	
	* Shorten variable name
	rename 	(mod_strata_EXO  	ana_ratio_treatment_all_EXO) ///
			(mod_strata_x  		ana_x_all)
	
	* Convert ratio to 100
	foreach v in ana_x_all {
	replace `v' = `v' * 100 if !missing(`v')
	}
	
* (2) Regression ---------------------------------------------------------------
	
	gen character = .
	gen treatment_character = .	
	gen cluster = .
	
		* Loop through variables
		mat SPL = J(1,12, .)
		
		foreach v in ana_x_all   { 
		foreach period in 1_4 5_8 9_12 {
		foreach outcome in NLU DAR OI AI {
			
			* Set of covariates

				gl covariates ${picked5a}
			
			* Create double interaction terms
			
				* Storing the variable we are testing against
				qui: replace character = `v'
				
				* Interactions
				qui: replace treatment_character = treatment * character
				
				qui: sum `outcome' if (treatment==0 & months_`period' == 1)
				local CM = r(mean)
				
				* Regression
				qui: reg `outcome' ///
							treatment character treatment_character ///
							$covariates i.strata i.year_month ///
							if (months_`period' == 1), ///
							vce(cluster mod_strata_x)
				
				qui: test treatment
				mat P1 =  _b[treatment], _se[treatment], r(p)
				
				qui: test character
				mat P2 =  _b[character], _se[character], r(p)
				
				qui: test treatment_character
				mat P3 =  _b[treatment_character], _se[treatment_character], r(p), e(N)
				
				local N: di %9.0fc e(N)
				mat SPL_`v'_`outcome'_months_`period'_t = P1, ., P3, `CM', P2
				
				mat SPL = SPL \ SPL_`v'_`outcome'_months_`period'_t
				

			
		}
		}
		}
	
	mat li SPL
	
* Table B27 --------------------------------------------------------------------

	table_start_horizontal_het_panel ///
	${report_folder}/Admin_App_het_monthly_ratio_ANA.txt ///
	"Spillover Effects on Key Field Outcomes" 150 Ratio 
	file write table  ///
			" &  & & \multicolumn{3}{c}{\textbf{Sit-D}} &  & \multicolumn{3}{c}{\begin{tabular}[c]{c@{}c@{}} \textbf{Sit-D $ \times $} \\ \textbf{Ratio} \end{tabular} } \\  " _n ///
			"\hhline{ ~~~===~===}" _n ///
			"\\[-1em]" _n ///
			" & & &  \multicolumn{1}{c}{Coef} &  \multicolumn{1}{c}{SE} &  \multicolumn{1}{c}{p-value} & &  \multicolumn{1}{c}{Coef} &  \multicolumn{1}{c}{SE} &  \multicolumn{1}{c}{p-value} \\ &   & &  \multicolumn{1}{c}{(1)} &  \multicolumn{1}{c}{(2)} &  \multicolumn{1}{c}{(3)} & &  \multicolumn{1}{c}{(4)} &  \multicolumn{1}{c}{(5)} &  \multicolumn{1}{c}{(6)} \\ " _n ///
			"\hhline{ ~~~---~---} \\[-1em]" _n

		write_horizontal_het_one 	SPL_ana_x_all_NLU_months_1_4_t			1	1000	"Uses of non-lethal force"
		write_horizontal_het_one 	SPL_ana_x_all_DAR_months_1_4_t			1	1000	"Discretionary arrests" 
		
	local N : di %9.0fc SPL_ana_x_all_AI_months_1_4_t[1,8]
	write_end end_spill_over_ANA `N'
	table_end "$end_spill_over_ANA" Admin_App_het_monthly_ratio_ANA

	
* Table B28 --------------------------------------------------------------------

	table_start_horizontal_het_panel ///
	${report_folder}/Admin_App_het_monthly_ratio_5_8_9_12_ANA.txt ///
	"Spillover Effects on Field Outcomes in Additional Periods" 150 Ratio 
	file write table "\multicolumn{10}{c}{\textbf{Panel A: Spillover Effects in Months 5-8 }}  \\" _n ///
			" &  & & \multicolumn{3}{c}{\textbf{Sit-D}} &  & \multicolumn{3}{c}{\begin{tabular}[c]{c@{}c@{}} \textbf{Sit-D $ \times $} \\ \textbf{Ratio} \end{tabular} } \\  " _n ///
			"\hhline{ ~~~===~===}" _n ///
			"\\[-1em]" _n ///
			" & & &  \multicolumn{1}{c}{Coef} &  \multicolumn{1}{c}{SE} &  \multicolumn{1}{c}{p-value} & &  \multicolumn{1}{c}{Coef} &  \multicolumn{1}{c}{SE} &  \multicolumn{1}{c}{p-value} \\ &   & &  \multicolumn{1}{c}{(1)} &  \multicolumn{1}{c}{(2)} &  \multicolumn{1}{c}{(3)} & &  \multicolumn{1}{c}{(4)} &  \multicolumn{1}{c}{(5)} &  \multicolumn{1}{c}{(6)} \\ " _n ///
			"\hhline{ ~~~---~---} \\[-1em]" _n

		write_horizontal_het_one 	SPL_ana_x_all_NLU_months_5_8_t			1	1000	"Uses of non-lethal force"
		write_horizontal_het_one 	SPL_ana_x_all_DAR_months_5_8_t			1	1000	"Discretionary arrests" 
		
	
	file write table "\\" _n 
		midrule
	
	file write table "\multicolumn{10}{c}{\textbf{Panel B: Spillover Effects in Months 9-12}}  \\" _n ///
			" &  & & \multicolumn{3}{c}{\textbf{Sit-D}} &  & \multicolumn{3}{c}{\begin{tabular}[c]{c@{}c@{}} \textbf{Sit-D $ \times $} \\ \textbf{Ratio} \end{tabular} } \\  " _n ///
			"\hhline{ ~~~===~===}" _n ///
			"\\[-1em]" _n ///
			" & & &  \multicolumn{1}{c}{Coef} &  \multicolumn{1}{c}{SE} &  \multicolumn{1}{c}{p-value} & &  \multicolumn{1}{c}{Coef} &  \multicolumn{1}{c}{SE} &  \multicolumn{1}{c}{p-value} \\ &   & &  \multicolumn{1}{c}{(1)} &  \multicolumn{1}{c}{(2)} &  \multicolumn{1}{c}{(3)} & &  \multicolumn{1}{c}{(4)} &  \multicolumn{1}{c}{(5)} &  \multicolumn{1}{c}{(6)} \\ " _n ///
			"\hhline{ ~~~---~---} \\[-1em]" _n

	
		write_horizontal_het_one_cm 	SPL_ana_x_all_NLU_months_9_12_t			1	1000	"Uses of non-lethal force"
		write_horizontal_het_one_cm 	SPL_ana_x_all_DAR_months_9_12_t			1	1000	"Discretionary arrests" 
		
		
	local N1 : di %9.0fc SPL_ana_x_all_NLU_months_5_8_t[1,8]
	local N2 : di %9.0fc SPL_ana_x_all_NLU_months_9_12_t[1,8]
	
	write_end end_spill_over_ANA_extra `N1' `N2'
	table_end "$end_spill_over_ANA_extra" Admin_App_het_monthly_ratio_ANA_extra

